//
//  CCCollectionAdapter.m
//  CKApp
//
//  Created by CC on 2020/9/8.
//  Copyright © 2020 This Moment. All rights reserved.
//

#import "LcFoundation.h"
#import "CCCollectionAdapter.h"
#import "CCCollectionViewCellData.h"

@interface CCCollectionAdapter () <UICollectionViewDelegate,
                                   UICollectionViewDataSource,
                                   UICollectionViewDelegateFlowLayout>

@end

@implementation CCCollectionAdapter

- (void)registerCollectionView:(UICollectionView*)collectionView
               scrollDirection:(UICollectionViewScrollDirection)scrollDirection {
  self.collectionView = collectionView;
  self.collectionView.delegate = self;
  self.collectionView.dataSource = self;
  UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc] init];
  flowLayout.scrollDirection = scrollDirection;
}

- (void)autoRegisterCellByCellData:(UICollectionView*)collectionView {
  NSMutableSet* cellClassesSet = [NSMutableSet set];
  for (NSInteger i = 0; i < self.cellDatas.count; i++) {
    NSArray* datas = self.cellDatas[i];
    for (CCCollectionViewCellData* cellData in datas) {
      NSString* cellName = [cellData nibName];
      if (![cellClassesSet containsObject:cellName]) {
        [self.collectionView registerNib:[UINib nibWithNibName:cellName bundle:nil]
              forCellWithReuseIdentifier:cellName];
      }
      [cellClassesSet addObject:cellName];
    }
  }
}

- (void)removeAllCellDatas {
  [self.cellDatas removeAllObjects];
}

- (void)refreshAllCells {
  [self.collectionView reloadData];
}

- (void)refreshCellData:(CCCollectionViewCellData*)cellData section:(NSInteger)section {
  LC_CHECK_IS_MAIN_THREAD();
  NSArray* cellDatasInSection = self.cellDatas[section];
  NSInteger row = [cellDatasInSection indexOfObject:cellData];
  [self.collectionView
      reloadItemsAtIndexPaths:@[ [NSIndexPath indexPathForRow:row inSection:section] ]];
}

- (CCCollectionViewCell*)cellWithCellData:(CCCollectionViewCellData*)cellData
                                  section:(NSInteger)section {
  NSArray* cellDatasInSection = self.cellDatas[section];
  NSInteger row = [cellDatasInSection indexOfObject:cellData];
  return (CCCollectionViewCell*)[self.collectionView
      cellForItemAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];
}

- (CCCollectionViewCellData*)cellData:(NSIndexPath*)indexPath {
  if (self.cellDatas.count > indexPath.section) {
    NSArray* items = self.cellDatas[indexPath.section];
    if (items.count > indexPath.row) {
      return items[indexPath.row];
    }
  }
  LC_FAIL();
  return nil;
}

#pragma mark - UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView {
  return self.cellDatas.count;
}

- (NSInteger)collectionView:(UICollectionView*)collectionView
     numberOfItemsInSection:(NSInteger)section {
  if (self.cellDatas.count > section) {
    return self.cellDatas[section].count;
  }
  return 0;
}

- (CGSize)collectionView:(UICollectionView*)collectionView
                    layout:(UICollectionViewLayout*)collectionViewLayout
    sizeForItemAtIndexPath:(NSIndexPath*)indexPath {
  CCCollectionViewCellData* cellData = [self cellData:indexPath];
  return cellData.cellSize;
}

- (UIEdgeInsets)collectionView:(UICollectionView*)collectionView
                        layout:(UICollectionViewLayout*)collectionViewLayout
        insetForSectionAtIndex:(NSInteger)section {
  if (self.sectionInsets.count > section) {
    return [self.sectionInsets[section] UIEdgeInsetsValue];
  }
  return UIEdgeInsetsZero;
}

- (CGFloat)collectionView:(UICollectionView*)collectionView
                                 layout:(UICollectionViewLayout*)collectionViewLayout
    minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  if (self.minimumLineSpacing.count > section) {
    return [self.minimumLineSpacing[section] floatValue];
  }
  return 0.0;
}

- (CGFloat)collectionView:(UICollectionView*)collectionView
                                      layout:(UICollectionViewLayout*)collectionViewLayout
    minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  if (self.minimumInteritemSpacing.count > section) {
    return [self.minimumInteritemSpacing[section] floatValue];
  }
  return 0.0;
}

- (CGSize)collectionView:(UICollectionView*)collectionView
                             layout:(UICollectionViewLayout*)collectionViewLayout
    referenceSizeForHeaderInSection:(NSInteger)section {
  if (self.headerReferenceSize.count > section) {
    return [self.headerReferenceSize[section] CGSizeValue];
  }
  return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView*)collectionView
                             layout:(UICollectionViewLayout*)collectionViewLayout
    referenceSizeForFooterInSection:(NSInteger)section {
  if (self.footerReferenceSize.count > section) {
    return [self.footerReferenceSize[section] CGSizeValue];
  }
  return CGSizeZero;
}

- (void)collectionView:(UICollectionView*)collectionView
    didSelectItemAtIndexPath:(NSIndexPath*)indexPath {
  CCCollectionViewCellData* cellData = [self cellData:indexPath];
  if (cellData.onCellClickedHandler != nil) {
    cellData.onCellClickedHandler(cellData);
  }
}

- (__kindof UICollectionViewCell*)collectionView:(UICollectionView*)collectionView
                          cellForItemAtIndexPath:(NSIndexPath*)indexPath {
  CCCollectionViewCellData* cellData = [self cellData:indexPath];
  NSString* nibName = [cellData nibName];
  UICollectionViewCell* cell =
      [collectionView dequeueReusableCellWithReuseIdentifier:nibName forIndexPath:indexPath];
  [cellData refreshCell:(CCCollectionViewCell*)cell animated:NO];
  return cell;
}

@end
